package com.csyf.manager.config;

import com.csyf.manager.schedule.*;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: Enzo
 * @Date: 2018/5/7 10:21
 */
@Component
public class SchedulerCenterConifg {

    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    private Environment env;


    public void scheduleJobs() throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        startJob1(scheduler);
        startJob2(scheduler);
        startJob3(scheduler);
        startJob4(scheduler);
        startJob5(scheduler);
        startJob6(scheduler);
    }
    private void startJob1(Scheduler scheduler) throws SchedulerException{

        String[] activeProfiles = env.getActiveProfiles();
        List<String> strings = Arrays.asList(activeProfiles);
        String cron = "0 0 2 * * ?  ";
        if (strings.contains("prod")) {
            cron = "0 0 2 * * ?  ";
        }

        JobDetail jobDetail = JobBuilder.newJob(DayProcessSchedule.class) .withIdentity("job1", "group1").build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        TriggerKey triggerKey = new TriggerKey("trigger1", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();
        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }
    private void startJob2(Scheduler scheduler) throws SchedulerException{
        JobKey jobKey = JobKey.jobKey("job2", "group1");
        JobDetail jobDetail = JobBuilder.newJob(MinuteProcessSchedule.class) .withIdentity(jobKey).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");

        TriggerKey triggerKey = new TriggerKey("trigger2", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();

        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }


    private void startJob3(Scheduler scheduler) throws SchedulerException{
        JobKey jobKey = JobKey.jobKey("fivejob", "group1");
        JobDetail jobDetail = JobBuilder.newJob(FiveProcessSchedule.class) .withIdentity(jobKey).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/5 * * * ?");

        TriggerKey triggerKey = new TriggerKey("fivetrigger", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();

        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }

    private void startJob4(Scheduler scheduler) throws SchedulerException{
        JobKey jobKey = JobKey.jobKey("job4", "group1");
        JobDetail jobDetail = JobBuilder.newJob(ClearingProcessSchedule.class) .withIdentity(jobKey).build();
        //CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 10 21 * * ? ");
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 14 * * ? ");

        TriggerKey triggerKey = new TriggerKey("trigger4", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();

        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }


    private void startJob5(Scheduler scheduler) throws SchedulerException{
        JobKey jobKey = JobKey.jobKey("job5", "group1");
        JobDetail jobDetail = JobBuilder.newJob(RevertMoneySchedule.class) .withIdentity(jobKey).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 2 0 * * ? ");

        TriggerKey triggerKey = new TriggerKey("trigger5", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();

        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }


    private void startJob6(Scheduler scheduler) throws SchedulerException{
        JobKey jobKey = JobKey.jobKey("job6", "group1");
        JobDetail jobDetail = JobBuilder.newJob(AccountMoneyProcessSchedule.class) .withIdentity(jobKey).build();
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 30 0 * * ? ");

        TriggerKey triggerKey = new TriggerKey("trigger6", "group1");
        CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey) .withSchedule(scheduleBuilder).build();

        boolean b = scheduler.checkExists(triggerKey);

        if(b){
            scheduler.rescheduleJob(triggerKey,cronTrigger);
        }else{
            scheduler.scheduleJob(jobDetail,cronTrigger);
        }
    }

}
